package com.scenery.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.scenery.R;
import com.scenery.vo.City;

/**
 * 城市Adapter 
 * **/
public class CityAdapter extends ArrayAdapter<City>{
	private LayoutInflater inflater;
	
	protected ArrayList<City> items;
	public CityAdapter(Context context, int layout, ArrayList<City> items) {
		
		// TODO Auto-generated constructor stub
		super(context, layout, items);
		 inflater = LayoutInflater.from(context);
		 this.items = items;
	}
	 @Override
	    public int getCount() {
	        return items.size();
	    }
	    
	    @Override
	    public synchronized void clear() {
	        items.clear();
	        notifyDataSetChanged();
	    }
	    
	    @Override
	    public synchronized void add(City bean) {
	    	items.add(bean);
	    }
	    
		public synchronized void modify(int index, City bean){
			items.set(index, bean);
		}
	 @Override
	    public View getView(int position, View convertView, ViewGroup parent){
	    	
	    	ViewHolder holder;

	        if (convertView == null) {
	        	convertView = inflater.inflate(R.layout.city_list_item, null);
	        	
	        	holder = new ViewHolder();
	        	
	        	holder.name = (TextView) convertView.findViewById(R.id.cityName);
	        	
	        	convertView.setTag(holder);
	        }else{
	        	holder = (ViewHolder) convertView.getTag();
	        }
	        
	        City bean = items.get(position);
	        if (bean != null) {
	        	
	        	holder.name.setText(position+1+". "+bean.getName());
	        	
	        	
	        	
	        }
	        return convertView;
	    }
	
	 private static class ViewHolder{
	    	TextView name;
	    	
	    	
	    }
}
